/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.controller;

import java.io.Serializable;
import java.math.BigDecimal;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import model.db.ProjectsHistory;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import model.controller.exceptions.NonexistentEntityException;
import model.controller.exceptions.PreexistingEntityException;
import model.db.Projects;

/**
 *
 * @author mwiatrowski
 */
public class ProjectsJpaController implements Serializable {

    public ProjectsJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Projects projects) throws PreexistingEntityException, Exception {
        if (projects.getProjectsHistoryCollection() == null) {
            projects.setProjectsHistoryCollection(new ArrayList<ProjectsHistory>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Collection<ProjectsHistory> attachedProjectsHistoryCollection = new ArrayList<ProjectsHistory>();
            for (ProjectsHistory projectsHistoryCollectionProjectsHistoryToAttach : projects.getProjectsHistoryCollection()) {
                projectsHistoryCollectionProjectsHistoryToAttach = em.getReference(projectsHistoryCollectionProjectsHistoryToAttach.getClass(), projectsHistoryCollectionProjectsHistoryToAttach.getId());
                attachedProjectsHistoryCollection.add(projectsHistoryCollectionProjectsHistoryToAttach);
            }
            projects.setProjectsHistoryCollection(attachedProjectsHistoryCollection);
            em.persist(projects);
            for (ProjectsHistory projectsHistoryCollectionProjectsHistory : projects.getProjectsHistoryCollection()) {
                Projects oldIdProjectsOfProjectsHistoryCollectionProjectsHistory = projectsHistoryCollectionProjectsHistory.getIdProjects();
                projectsHistoryCollectionProjectsHistory.setIdProjects(projects);
                projectsHistoryCollectionProjectsHistory = em.merge(projectsHistoryCollectionProjectsHistory);
                if (oldIdProjectsOfProjectsHistoryCollectionProjectsHistory != null) {
                    oldIdProjectsOfProjectsHistoryCollectionProjectsHistory.getProjectsHistoryCollection().remove(projectsHistoryCollectionProjectsHistory);
                    oldIdProjectsOfProjectsHistoryCollectionProjectsHistory = em.merge(oldIdProjectsOfProjectsHistoryCollectionProjectsHistory);
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findProjects(projects.getIdProjects()) != null) {
                throw new PreexistingEntityException("Projects " + projects + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Projects projects) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Projects persistentProjects = em.find(Projects.class, projects.getIdProjects());
            Collection<ProjectsHistory> projectsHistoryCollectionOld = persistentProjects.getProjectsHistoryCollection();
            Collection<ProjectsHistory> projectsHistoryCollectionNew = projects.getProjectsHistoryCollection();
            Collection<ProjectsHistory> attachedProjectsHistoryCollectionNew = new ArrayList<ProjectsHistory>();
            for (ProjectsHistory projectsHistoryCollectionNewProjectsHistoryToAttach : projectsHistoryCollectionNew) {
                projectsHistoryCollectionNewProjectsHistoryToAttach = em.getReference(projectsHistoryCollectionNewProjectsHistoryToAttach.getClass(), projectsHistoryCollectionNewProjectsHistoryToAttach.getId());
                attachedProjectsHistoryCollectionNew.add(projectsHistoryCollectionNewProjectsHistoryToAttach);
            }
            projectsHistoryCollectionNew = attachedProjectsHistoryCollectionNew;
            projects.setProjectsHistoryCollection(projectsHistoryCollectionNew);
            projects = em.merge(projects);
            for (ProjectsHistory projectsHistoryCollectionOldProjectsHistory : projectsHistoryCollectionOld) {
                if (!projectsHistoryCollectionNew.contains(projectsHistoryCollectionOldProjectsHistory)) {
                    projectsHistoryCollectionOldProjectsHistory.setIdProjects(null);
                    projectsHistoryCollectionOldProjectsHistory = em.merge(projectsHistoryCollectionOldProjectsHistory);
                }
            }
            for (ProjectsHistory projectsHistoryCollectionNewProjectsHistory : projectsHistoryCollectionNew) {
                if (!projectsHistoryCollectionOld.contains(projectsHistoryCollectionNewProjectsHistory)) {
                    Projects oldIdProjectsOfProjectsHistoryCollectionNewProjectsHistory = projectsHistoryCollectionNewProjectsHistory.getIdProjects();
                    projectsHistoryCollectionNewProjectsHistory.setIdProjects(projects);
                    projectsHistoryCollectionNewProjectsHistory = em.merge(projectsHistoryCollectionNewProjectsHistory);
                    if (oldIdProjectsOfProjectsHistoryCollectionNewProjectsHistory != null && !oldIdProjectsOfProjectsHistoryCollectionNewProjectsHistory.equals(projects)) {
                        oldIdProjectsOfProjectsHistoryCollectionNewProjectsHistory.getProjectsHistoryCollection().remove(projectsHistoryCollectionNewProjectsHistory);
                        oldIdProjectsOfProjectsHistoryCollectionNewProjectsHistory = em.merge(oldIdProjectsOfProjectsHistoryCollectionNewProjectsHistory);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                BigDecimal id = projects.getIdProjects();
                if (findProjects(id) == null) {
                    throw new NonexistentEntityException("The projects with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(BigDecimal id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Projects projects;
            try {
                projects = em.getReference(Projects.class, id);
                projects.getIdProjects();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The projects with id " + id + " no longer exists.", enfe);
            }
            Collection<ProjectsHistory> projectsHistoryCollection = projects.getProjectsHistoryCollection();
            for (ProjectsHistory projectsHistoryCollectionProjectsHistory : projectsHistoryCollection) {
                projectsHistoryCollectionProjectsHistory.setIdProjects(null);
                projectsHistoryCollectionProjectsHistory = em.merge(projectsHistoryCollectionProjectsHistory);
            }
            em.remove(projects);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Projects> findProjectsEntities() {
        return findProjectsEntities(true, -1, -1);
    }

    public List<Projects> findProjectsEntities(int maxResults, int firstResult) {
        return findProjectsEntities(false, maxResults, firstResult);
    }

    private List<Projects> findProjectsEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Projects.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Projects findProjects(BigDecimal id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Projects.class, id);
        } finally {
            em.close();
        }
    }

    public int getProjectsCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Projects> rt = cq.from(Projects.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
